package service

import (
	"errors"
	"gitee.com/jabberwocky238/proj-backend/model"
)

var (
	ErrMusicNotFound = errors.New("[NocobaseService] music not found")
)

type NocobaseService struct {
}

func (NocobaseService) GetMusicMetaByUUID(uuid string) (*VOMusic, error) {
	db := model.GetDB()

	var dao model.NocobaseMusicmeta
	if err := db.Where("\"UUID\" = ?", uuid).First(&dao).Error; err != nil {
		return nil, err
	}
	// if dao.Properties is null
	return &VOMusic{
		UUID:       uuid,
		Title:      dao.Title,
		Properties: dao.Properties,
		ThumbUrl:   dao.ThumbUrl,
	}, nil
}

func (NocobaseService) GetMusicMetaByPage() ([]VOMusic, error) {
	db := model.GetDB()

	var dao []model.NocobaseMusicmeta
	if err := db.Limit(10).Find(&dao).Error; err != nil {
		return nil, err
	}
	var vo_list = make([]VOMusic, 0)
	// if dao.Properties is null
	for _, item := range dao {
		vo_list = append(vo_list, VOMusic{
			UUID:       item.UUID,
			Title:      item.Title,
			Properties: item.Properties,
			ThumbUrl:   item.ThumbUrl,
		})
	}
	return vo_list, nil
}

const ROOT = "E:/1-code/__temp__/my-nocobase-app/storage/uploads/"

func (NocobaseService) GetMusicBlobByUUID(uuid string) (string, error) {
	db := model.GetDB()
	// 运行裸sql
	var target string
	if err := db.Raw("SELECT * FROM hello5(?, ?)", uuid, "Blob").Row().Scan(&target); err != nil {
		return "", err
	}
	return ROOT + "music/" + target, nil
}
func (NocobaseService) GetMusicCoverByUUID(uuid string) (string, error) {
	db := model.GetDB()
	// 运行裸sql
	var target string
	if err := db.Raw("SELECT * FROM hello5(?, ?)", uuid, "Cover").Row().Scan(&target); err != nil {
		return "", err
	}
	return ROOT + "covers/" + target, nil
}
